package com.kingdee.signup;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKLocationManager;
import com.baidu.mapapi.MKPoiInfo;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MapView.LayoutParams;
import com.baidu.mapapi.MyLocationOverlay;
import com.baidu.mapapi.PoiOverlay;
import com.baidu.mapapi.Projection;
import com.kingdee.signup.adapter.AddressAdapter;
import com.kingdee.signup.model.SearchData;
import com.kingdee.signup.utils.CommonUtils;
import com.kingdee.signup.utils.ComparatorDistance;
import com.kingdee.signup.utils.NLog;
import com.kingdee.signup.utils.StringUtils;
import com.kingdee.signup.widget.LongClickOverlay;
import com.kingdee.signup.widget.PositionOverlay;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LocalPositionActivity extends MapActivity
  implements View.OnClickListener, AdapterView.OnItemClickListener
{
  List<SearchData> data;
  SearchData datas;
  GeoPoint geoPoint;
  private AddressAdapter mAdapter;
  private ImageView mBack;
  private Context mContext;
  private String mCurrentAddress;
  private long mLatitudeE6;
  private ListView mListView;
  LocationListener mLocationListener = null;
  MyLocationOverlay mLocationOverlay = null;
  private long mLongitudeE6;
  private MKSearch mMKSearch;
  private MKPoiResult mPoiResult;
  public View mPopView;
  private MapController mapController;
  private BMapManager mapManager;
  private MapView mapView;

  private void initPopview()
  {
    mPopView = super.getLayoutInflater().inflate(2130903070, null);
    mapView.addView(this.mPopView, new MapView.LayoutParams(-2, -2, null, 81));
    mPopView.setVisibility(8);
  }

  protected boolean isRouteDisplayed()
  {
    return false;
  }

  public void onClick(View paramView)
  {
    switch (paramView.getId())
    {
	    case 2131493009:
	        finish();
	    default:    
    }
  }

  public void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setContentView(2130903066);
    mBack = ((ImageView)findViewById(2131493009));
    mBack.setOnClickListener(this);
    mapManager = new BMapManager(getApplication());
    mContext = this;
    mapManager.init("285B415EBAB2A92293E85502150ADA7F03C777C4", null);
    super.initMapActivity(this.mapManager);
    mapView = ((MapView)findViewById(2131493010));
    mListView = ((ListView)findViewById(2131493011));
    mListView.setOnItemClickListener(this);
    mAdapter = new AddressAdapter(this);
    mListView.setAdapter(this.mAdapter);
    mapView.setTraffic(true);
    mapView.setBuiltInZoomControls(true);
    mapView.setDrawOverlayWhenZooming(true);
    initPopview();
    mLocationOverlay = new MyLocationOverlay(this, this.mapView);
    mapView.getOverlays().add(this.mLocationOverlay);
    mCurrentAddress = getIntent().getStringExtra("local_address");
    mLocationListener = new LocationListener()
    {
      public void onLocationChanged(Location paramLocation)
      {
        if (paramLocation != null)
        {
          geoPoint = new GeoPoint((int)(1000000.0D * paramLocation.getLatitude()), (int)(1000000.0D * paramLocation.getLongitude()));
          NLog.i("debug", "-----------" + geoPoint.getLatitudeE6() + "-------" + geoPoint.getLongitudeE6());
          mLatitudeE6 = geoPoint.getLatitudeE6();
          mLongitudeE6 = geoPoint.getLongitudeE6();
          mapView.getController().animateTo(geoPoint);
          mapController = mapView.getController();
          LongClickOverlay localLongClickOverlay = new LongClickOverlay(LocalPositionActivity.this);
          mapView.getOverlays().add(localLongClickOverlay);
          mapView.invalidate();
          mapController.setCenter(geoPoint);
          mapController.setZoom(16);
          mMKSearch = new MKSearch();
          MKSearch.setPoiPageCapacity(50);
          mMKSearch.init(mapManager, new MySearchListener());
          mMKSearch.poiSearchNearBy("大", geoPoint, 500);
        }
      }
    };
  }

  protected void onDestroy()
  {
    if (this.mapManager != null)
    {
      this.mapManager.destroy();
      this.mapManager = null;
    }
    super.onDestroy();
  }

  public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong)
  {
    List localList = this.mAdapter.getmDynamicList();
    Intent localIntent = new Intent();
    localIntent.setClass(this, SignProjectActivity.class);
    localIntent.putExtra("local_address_title", ((SearchData)localList.get(paramInt)).getAddress() + ((SearchData)localList.get(paramInt)).getName());
    double d1 = ((SearchData)localList.get(paramInt)).getLatitudeE6() / 1000000.0D;
    NLog.d("debug", "latitude_integer--------" + d1);
    double d2 = ((SearchData)localList.get(paramInt)).getLongitudeE6() / 1000000.0D;
    NLog.d("debug", "longitude--------" + d2);
    localIntent.putExtra("local_latitude", String.valueOf(d1));
    localIntent.putExtra("local_longitude", String.valueOf(d2));
    setResult(-1, localIntent);
    finish();
  }

  public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent)
  {
		boolean flag;
		if (paramInt == 4) {
			finish();
			flag = true;
		} else {
			flag = false;
		}
		return flag;
  }

  protected void onPause()
  {
    if (this.mapManager != null)
    {
      this.mapManager.getLocationManager().removeUpdates(this.mLocationListener);
      this.mLocationOverlay.disableMyLocation();
      this.mLocationOverlay.disableCompass();
      this.mapManager.stop();
    }
    super.onPause();
  }

  protected void onResume()
  {
    if (this.mapManager != null)
    {
      this.mapManager.getLocationManager().requestLocationUpdates(this.mLocationListener);
      this.mLocationOverlay.enableMyLocation();
      this.mLocationOverlay.enableCompass();
      this.mapManager.start();
    }
    super.onResume();
  }

  public void showPopupWindow(int paramInt1, int paramInt2)
  {
    GeoPoint localGeoPoint = this.mapView.getProjection().fromPixels(paramInt1, paramInt2);
    long l1 = localGeoPoint.getLatitudeE6();
    long l2 = localGeoPoint.getLongitudeE6();
    NLog.d("debug", "alan-----测试" + l1 + "(经度)" + l2 + "(纬度)");
    if ((int)StringUtils.GetShortDistance(this.mLongitudeE6, this.mLatitudeE6, l2, l1) <= 500)
      this.mapView.getOverlays().add(new PositionOverlay(localGeoPoint, this, 2130837567));
    while (true)
    {
      Toast.makeText(mContext, "只能选择500米范围以内的地址标注", 1).show();
      return;
    }
  }

  class MyOverlay extends PoiOverlay
  {
    private ArrayList<MKPoiInfo> mInfo;

    public MyOverlay(Activity paramMapView, MapView view)
    {  
      super(paramMapView,view);
    }

    protected boolean onTap(int paramInt)
    {
      MapView.LayoutParams localLayoutParams = (MapView.LayoutParams)mPopView.getLayoutParams();
      localLayoutParams.point = ((MKPoiInfo)this.mInfo.get(paramInt)).pt;
      mapView.updateViewLayout(mPopView, localLayoutParams);
      mPopView.setVisibility(0);
      TextView localTextView = (TextView)findViewById(2131493030);
      localTextView.setTextColor(Color.parseColor("#000000"));
      localTextView.setText(((MKPoiInfo)this.mInfo.get(paramInt)).name);
      return true;
    }

    public boolean onTap(GeoPoint paramGeoPoint, MapView paramMapView)
    {
      mPopView.setVisibility(8);
      return super.onTap(paramGeoPoint, paramMapView);
    }

    public void setData(ArrayList<MKPoiInfo> paramArrayList)
    {
      super.setData(paramArrayList);
      this.mInfo = paramArrayList;
    }
  }

  public class MySearchListener implements MKSearchListener
  {
    public MySearchListener()
    {
    	super();
    }

    public void onGetAddrResult(MKAddrInfo paramMKAddrInfo, int paramInt)
    {
      if (paramMKAddrInfo == null)
        NLog.d("debug", "没有获取想要的位置");
      while (true)
      {
    	NLog.d("debug", "地址：" + paramMKAddrInfo.strAddr);
        return;
      }
    }

    public void onGetDrivingRouteResult(MKDrivingRouteResult paramMKDrivingRouteResult, int paramInt)
    {
    }

    public void onGetPoiResult(MKPoiResult paramMKPoiResult, int paramInt1, int paramInt2)
    {
      if (paramMKPoiResult == null)
      {
        mPoiResult = null;
        data = new ArrayList();
        SearchData localSearchData3 = new SearchData();
        localSearchData3.setName(mCurrentAddress);
        localSearchData3.setAddress(mCurrentAddress);
        localSearchData3.setLatitudeE6(mLatitudeE6);
        localSearchData3.setLongitudeE6(mLongitudeE6);
        localSearchData3.setDistance(0.0D);
        data.add(localSearchData3);
        mAdapter.addDataList(data);
        mAdapter.notifyDataSetChanged();
        return;
      }
      MyOverlay localMyOverlay = new MyOverlay(LocalPositionActivity.this,mapView);
      localMyOverlay.setData(paramMKPoiResult.getAllPoi());
      mPoiResult = paramMKPoiResult;
      data = new ArrayList();
      if (data.size() > 0)
        data.clear();
      SearchData localSearchData1 = new SearchData();
      localSearchData1.setName("");
      localSearchData1.setAddress(mCurrentAddress);
      localSearchData1.setLatitudeE6(mLatitudeE6);
      localSearchData1.setLongitudeE6(mLongitudeE6);
      localSearchData1.setDistance(0.0D);
      data.add(localSearchData1);
       
      //读取有用信息
      SearchData localSearchData3 = new SearchData();
      localSearchData3.setName("");
      localSearchData3.setAddress(CommonUtils.getStringByKey(mContext, "MyAdd"));
      localSearchData3.setLatitudeE6(Long.parseLong(CommonUtils.getStringByKey(mContext, "Lat")));
      localSearchData3.setLongitudeE6(Long.parseLong(CommonUtils.getStringByKey(mContext, "Lon")));
      localSearchData3.setDistance(1.0D);
      data.add(localSearchData3);  
      
      for (int i = 0; ; i++)
      {
        if (i >= paramMKPoiResult.getAllPoi().size())
        {
          ComparatorDistance localComparatorDistance = new ComparatorDistance();
          Collections.sort(data, localComparatorDistance);
          mapView.getOverlays().add(localMyOverlay);
          mAdapter.addDataList(data);
          mAdapter.notifyDataSetChanged();
          break;
        }
        NLog.i("debug", "-----------数据：" + ((MKPoiInfo)paramMKPoiResult.getAllPoi().get(i)).name + " 地址：" + ((MKPoiInfo)paramMKPoiResult.getAllPoi().get(i)).address + "纬度：" + ((MKPoiInfo)paramMKPoiResult.getAllPoi().get(i)).pt.getLatitudeE6() + " 经度：" + ((MKPoiInfo)paramMKPoiResult.getAllPoi().get(i)).pt.getLongitudeE6());
        SearchData localSearchData2 = new SearchData();
        localSearchData2.setName(((MKPoiInfo)paramMKPoiResult.getAllPoi().get(i)).name);
        localSearchData2.setAddress(((MKPoiInfo)paramMKPoiResult.getAllPoi().get(i)).address);
        localSearchData2.setLatitudeE6(((MKPoiInfo)paramMKPoiResult.getAllPoi().get(i)).pt.getLatitudeE6());
        localSearchData2.setLongitudeE6(((MKPoiInfo)paramMKPoiResult.getAllPoi().get(i)).pt.getLongitudeE6());
        localSearchData2.setDistance(StringUtils.GetShortDistance(mLongitudeE6, mLatitudeE6, ((MKPoiInfo)paramMKPoiResult.getAllPoi().get(i)).pt.getLongitudeE6(), ((MKPoiInfo)paramMKPoiResult.getAllPoi().get(i)).pt.getLatitudeE6()));
        data.add(localSearchData2);
      }
    }

    public void onGetTransitRouteResult(MKTransitRouteResult paramMKTransitRouteResult, int paramInt)
    {
    }

    public void onGetWalkingRouteResult(MKWalkingRouteResult paramMKWalkingRouteResult, int paramInt)
    {
    }
  }
}